<template>
	<view class="body">
		<!-- 添加好友信息卡片 -->
		<view class="cart" v-for="(item,index) in addUserList" :key="index">	
			<!-- 用户头像 -->
			<view class="userimg">
				<u-avatar shape="circle" size="60" :src="item.sendUserImg">
				</u-avatar>
			</view>
			<!-- 用户名称 -->
			<view class="issueUser">
				<text>{{item.sendUserName}}</text>
			</view>
			<!-- 用户性别 -->
			<view class="sex">
				<view class="tb">
					<text class="t-icon t-icon-xingbienan" style="width: 40rpx;height: 40rpx;" v-if="item.sex == 0"></text>
					<text class="t-icon t-icon-xingbienan-copy" style="width: 40rpx;height: 40rpx;"  v-if="item.sex == 1"></text>
				</view>
				<text class="sextext" v-if="item.sex == 0">女</text> 
				<text class="sextext" v-if="item.sex == 1">男</text>
			</view>
			<!-- 附加信息 -->
			<view class="content">
				<text>附加信息:请求加为好友</text>
			</view>
			<!-- 来源 -->
			<view class="laiyuan">
				<text>来源:   ID查找</text>
			</view>
			<view class="button" @click="adduser(index)" v-if="item.isRead == false">
				<text>同意</text>
			</view>
			<view class="button1"  v-if="item.isRead">
				<text>已同意</text>
			</view>
			<u-line margin="10px 0px 0px 0px" hairline="true"></u-line>
		</view>
	</view>
</template>

<script>
	import {http} from '../static/service/requestAddress/rtas.js'
	export default {
		data() {  
			return {
				addUserList:[]
			}
		},
		onShow(){
			//获取缓存中的添加好友信息
			var addlist = []
			if(uni.getStorageSync("addUserMessage")){
				addlist = JSON.parse(uni.getStorageSync("addUserMessage"))
				for(var i =0;i<addlist.length;i++){
					addlist[i].sex =addlist[i].sendUser.slice(addlist[i].sendUser.indexOf(":")+1,7)
					console.log("截取的是："+addlist[i].sendUser.slice(addlist[i].sendUser.indexOf(":")+1,7))
				}
				for(var i =0;i<addlist.length-1;i++){
					for(var j =i+1;j<addlist.length;j++){
						if(addlist[j].sendUserName==addlist[i].sendUserName){
							addlist.splice(j,1)
							j--
						}
					}
				}
				
				 this.addUserList = addlist
				console.log(JSON.stringify(this.addUserList))
			}
		},
		methods:{
			adduser(index){
				var touserid = this.addUserList[index].sendUser.slice(0,this.addUserList[index].sendUser.indexOf(":"))
				var userlist = []
				if(uni.getStorageSync("user")){
					if(uni.getStorageSync("userfriendlist")){
						userlist = JSON.parse(uni.getStorageSync("userfriendlist"))
						this.addUserList[index].isRead = true
						//向redis中添加用户好友列表id
						uni.request({
							url:http+"/user/adduserfriend/"+JSON.parse(uni.getStorageSync("user")).id+"/"+touserid,
							method:'PUT',
							complete:(res)=>{
								if(res.data.code == 20041){
									var userInfo = {
										id:"",
										name:"",
										img:"",
										isStranger:false,//是否是陌生人
										lastmessage:"",
										totalMessage:0
									}
									userInfo.id = touserid
									userInfo.name = this.addUserList[index].sendUserName
									userInfo.img = this.addUserList[index].sendUserImg
									userlist.push(userInfo)
									uni.setStorageSync("userfriendlist",JSON.stringify(userlist))
									//更新添加好友缓存
									uni.setStorageSync("addUserMessage",JSON.stringify(this.addUserList))
									//设置缓存
									var num = uni.getStorageSync("tabbar")
									num = num -1
									if(num  > 0){
										uni.setTabBarBadge({ //显示数字
											index: 1, //tabbar下标
											text:""+num,//数字
											success() {
											console.log("消息设置成功") 
											},
											fail(e){ 
												 console.log("设置失败"+JSON.stringify(e))
											}
										})
									}else{
										uni.removeTabBarBadge({
											index:1
										})
									}
									uni.setStorageSync("tabbar",num)
									uni.$u.toast("添加好友成功")
								}else{
									uni.$u.toast('通过好友验证失败')
								}
							}
						})
					}else{
						userlist.push(touserid)
						uni.setStorageSync("userfriendlist",JSON.stringify(userlist))
					}
				}else{
					uni.$u.toast("登录后才能添加好友喔")
				}
			}
		},
	}
</script>

<style>
	@import url("../static/css/componentsCss/addUser/index.css");
</style>